package com.jss.lanouhn.fristapp.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.Interface.OnItemClickLister;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.CategorySampleInfo;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lanouhn on 16/8/26.
 */
public class CategorySampleItemAdapter extends RecyclerView.Adapter<CategorySampleItemAdapter.SampleItemViewHolder> {
    private Context mContext;
    private List<CategorySampleInfo.DataBean.CategoriesBean.SubcategoriesBean> subcategoriesBeen;
    private OnItemClickLister onItemClickLister;
    DisplayMetrics dm;

    public void setOnItemClickLister(OnItemClickLister onItemClickLister){
        this.onItemClickLister=onItemClickLister;
    }

    public CategorySampleItemAdapter(Context mContext, List<CategorySampleInfo.DataBean.CategoriesBean.SubcategoriesBean> subcategoriesBeen) {
        this.mContext = mContext;
        this.subcategoriesBeen = subcategoriesBeen;
        dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
    }

    @Override
    public SampleItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv_item_rv_sample, null);

        //动态设置ImageView的宽高，根据自己每行item数量计算
        //dm.widthPixels-dip2px(20)即屏幕宽度-左右10dp+10dp=20dp再转换为px的宽度，最后/3得到每个item的宽高
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((dm.widthPixels - dip2px(20)) / 3, (dm.widthPixels - dip2px(20)) / 3);
        view.setLayoutParams(lp);
        SampleItemViewHolder holder = new SampleItemViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final SampleItemViewHolder holder, final int position) {
        CategorySampleInfo.DataBean.CategoriesBean.SubcategoriesBean bean = subcategoriesBeen.get(position);
        holder.tv_sample.setText(bean.getName());
        Picasso.with(mContext).load(bean.getIcon_url()).into(holder.iv_sample);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos=holder.getLayoutPosition();
                onItemClickLister.onItemClick(holder.itemView,pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return subcategoriesBeen.size();
    }


    public class SampleItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_sample;
        private TextView tv_sample;

        public SampleItemViewHolder(View itemView) {
            super(itemView);
            tv_sample = (TextView) itemView.findViewById(R.id.tv_item_item_sample);
            iv_sample = (ImageView) itemView.findViewById(R.id.iv_item_item_sample);

        }

    }
    int dip2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
